package com.xudow.app.newui.task;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.util.Log;

import com.activeshare.edu.ucenter.common.messages.course.ListCourseMessage;
import com.google.gson.Gson;
import com.xudow.app.Config;
import com.xudow.app.network.AjaxCallBack;
import com.xudow.app.network.OkHttpClientManager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：LownChen on 2016/10/20 11:00
 */
public class CourseTask {

    Handler courseListHandler;
    HashMap<String, String> params;

         OkHttpClientManager   ok=new OkHttpClientManager();
    FragmentActivity activity;
    public CourseTask(FragmentActivity activity, Handler courseListHandler, HashMap<String, String> params) {
          this.activity=activity;
         this.courseListHandler=courseListHandler;
        this.params=params;

    }

    public static final int SUCCESS = 0;
    public static final int ERROR = 1;
    private void sendSuccessMessage(List<ListCourseMessage.CourseListEntity> courses) {
        android.os.Message message = new android.os.Message();
        message.what = SUCCESS;

        Bundle data = new Bundle();

        if (courses != null) {
            ListCourseMessage.CourseListEntity[] courseArr = new ListCourseMessage.CourseListEntity[courses.size()];
            for (int i = 0; i < courses.size(); i++) {
                courseArr[i] = courses.get(i);
            }

            data.putSerializable("courses", courseArr);

            message.setData(data);
        }

        courseListHandler.sendMessage(message);
    }

    private void sendErrorMessage(String msg) {
        android.os.Message message = new android.os.Message();
        message.what = ERROR;

        if (null != msg) {
            Bundle data = new Bundle();
            data.putString("errMessage", msg);
            message.setData(data);
        }

        courseListHandler.sendMessage(message);
    }

    public void CourseList(String url){

        Map<String,String> map=new HashMap<>();

//        if(map.get("page").equals(map.get("page"))){
//        }else{
        initMap(map);


        ok.getAsyn(activity,Config.BASE_URL +  url, map, new AjaxCallBack() {
            @Override
            public void onSucces(String response) {

                ListCourseMessage listCourseMessage = null;
                try {

                    listCourseMessage = new Gson().fromJson(response.toString(), ListCourseMessage.class);

                    if (listCourseMessage != null) {

                        //FileUtils.writeFile(result1,new File(context.getCacheDir().getAbsolutePath(),"courseList1.txt"));
                        if (listCourseMessage.getResult().equals("0")) {

                            sendSuccessMessage(listCourseMessage.getCourseList());

                        } else if (listCourseMessage.getResult().equals("1")) {

                            sendErrorMessage(listCourseMessage.getErrorMessage());

                        }
                    } else {

                        sendErrorMessage(null);

                    }

                } catch (Exception e) {
                    sendErrorMessage(e.getMessage());

//                      Log.i("listCourseMessage","listCourseMessage>>"+listCourseMessage);
//                      Log.i("listCourseMessage","listCourseMessageeee>>"+e);
                    sendErrorMessage(null);

                }
            }

            @Override
            public void onFail(String response) {
                sendErrorMessage(null);
            }
          });

//        }
    }

    private void initMap(Map<String, String> map) {
        //关键字
        if (!android.text.TextUtils.isEmpty(params.get("coursename"))) {
            map.put("coursename", params.get("coursename"));
            Log.i("CourseListTask", "coursename>>>>" + params.get("coursename"));
        }
        //分页条数
        if (!android.text.TextUtils.isEmpty(params.get("offset"))) {
            map.put("offset", params.get("offset"));
            Log.i("CourseListTask", "offset>>>>" + params.get("offset"));
        }
        //第几页
        if (!android.text.TextUtils.isEmpty(params.get("page"))) {
            map.put("page", params.get("page"));
            Log.i("CourseListTask", "page>>>>" + params.get("page"));
        }
        //年级
        if (!android.text.TextUtils.isEmpty(params.get("gradeids"))) {
            map.put("gradeids", params.get("gradeids"));
            Log.i("CourseListTask", "gradeids>>>>" + params.get("gradeids"));
        }
        //兴趣
        /*if (!android.text.TextUtils.isEmpty(params.get("subjectids"))) {
            valueMap.add("subjectids", params.get("subjectids"));
        }*/
        //查询类别
        if (!android.text.TextUtils.isEmpty(params.get("searchtype"))) {
            map.put("searchtype", params.get("searchtype"));
            Log.i("CourseListTask", "searchtype>>>>" + params.get("searchtype"));
        }
        //排序类别
        if (!android.text.TextUtils.isEmpty(params.get("sorttype"))) {
            map.put("sorttype", params.get("sorttype"));
            Log.i("CourseListTask", "sorttype>>>>" + params.get("sorttype"));
        }
        //发布者编号
        if (!android.text.TextUtils.isEmpty(params.get("sendUserid"))) {
            map.put("sendUserid", params.get("sendUserid"));
            Log.i("CourseListTask", "sendUserid>>>>" + params.get("sendUserid"));
        }
        //最后一次查看时间
        if (!android.text.TextUtils.isEmpty(params.get("timestamp"))) {
            map.put("timestamp", params.get("timestamp"));
            Log.i("CourseListTask", "timestamp>>>>" + params.get("timestamp"));
        }
        //一级分类
        if (!android.text.TextUtils.isEmpty(params.get("level_one"))) {
            map.put("level_one", params.get("level_one"));
            Log.i("CourseListTask", "level_one>>>>" + params.get("level_one"));
        }

        //二级分类
        if (!android.text.TextUtils.isEmpty(params.get("level_two"))) {
            map.put("level_two", params.get("level_two"));
            Log.i("CourseListTask", "level_two>>>>" + params.get("level_two"));
        }

        //三级分类
        if (!android.text.TextUtils.isEmpty(params.get("level_three"))) {
            map.put("level_three", params.get("level_three"));
            Log.i("CourseListTask", "level_three>>>>" + params.get("level_three"));
        }
        String jw = "0.0";
        //经度
        if (params.get("longitude") != jw) {
            map.put("longitude", params.get("longitude"));
            Log.i("CourseListTask", "longitude>>>>" + params.get("longitude"));
        }

        //纬度
        if (params.get("latitude") != jw) {
            map.put("latitude", params.get("latitude"));
            Log.i("CourseListTask", "latitude>>>>" + params.get("latitude"));
        }

        //地区id
        if (!android.text.TextUtils.isEmpty(params.get("area_id"))) {
            map.put("area_id", params.get("area_id"));
            Log.i("CourseListTask", "area_id>>>>" + params.get("area_id"));
        }
        //学校Id
        if (!android.text.TextUtils.isEmpty(params.get("school_id"))) {
            map.put("school_id", params.get("school_id"));
            Log.i("CourseListTask", "school_id>>>>" + params.get("school_id"));
        }
        //课程类别
        if (!android.text.TextUtils.isEmpty(params.get("course_type"))) {
            map.put("course_type", params.get("course_type"));
            Log.i("CourseListTask", "course_type>>>>" + params.get("course_type"));
        }


//        Log.i("tiaojian2","schoolId"+params.get("schoolId")+"agencyId"+params.get("agencyId"));


        if (!android.text.TextUtils.isEmpty(params.get("areaId"))) {
            map.put("areaId", params.get("areaId"));
            Log.i("CourseListTask", "areaId>>>>" + params.get("areaId"));
        }

        if (!android.text.TextUtils.isEmpty(params.get("townId"))) {
            map.put("townId", params.get("townId"));
            Log.i("CourseListTask", "townId>>>>" + params.get("townId"));
        }
        if (!android.text.TextUtils.isEmpty(params.get("schoolId"))) {
            map.put("schoolId", params.get("schoolId"));
            Log.i("CourseListTask", "schoolId>>>>" + params.get("schoolId"));
        }

        if (!android.text.TextUtils.isEmpty(params.get("agencyId"))) {
            map.put("agencyId", params.get("agencyId"));
            Log.i("CourseListTask", "agencyId>>>>" + params.get("agencyId"));
        }


        if (!android.text.TextUtils.isEmpty(params.get("week"))) {
            map.put("week", params.get("week"));
            Log.i("CourseListTask", "week>>>>" + params.get("week"));
        }


        if (!android.text.TextUtils.isEmpty(params.get("begindateId"))) {
            map.put("begindateId", params.get("begindateId"));

            Log.i("CourseListTask", "begindateId>>>>" + params.get("begindateId"));
        }
        if (!android.text.TextUtils.isEmpty(params.get("gradeid"))) {
            map.put("gradeid", params.get("gradeid"));
            Log.i("CourseListTask", "gradeid>>>>" + params.get("gradeid"));

        }
        if (!android.text.TextUtils.isEmpty(params.get("coursePriceId"))) {
            map.put("coursePriceId", params.get("coursePriceId"));
            Log.i("CourseListTask", "coursePriceId>>>>" + params.get("coursePriceId"));

        }
        if (!android.text.TextUtils.isEmpty(params.get("semesterid"))) {
            map.put("semesterid", params.get("semesterid"));
            Log.i("CourseListTask", "semesterid>>>>" + params.get("semesterid"));

        }
        if (!android.text.TextUtils.isEmpty(params.get("enrolling"))) {
            map.put("enrolling", params.get("enrolling"));
            Log.i("CourseListTask", "enrolling>>>>" + params.get("enrolling"));

        }


        if (!android.text.TextUtils.isEmpty(params.get("allTowns"))) {
            map.put("allTowns", "at");
            Log.i("CourseListTask", "allTowns>>>>" + params.get("allTowns"));

        }
        if (!android.text.TextUtils.isEmpty(params.get("allClasses"))) {
            map.put("allClasses", "ac");
            Log.i("CourseListTask", "allClasses>>>>" + params.get("allClasses"));

        }
        if (!android.text.TextUtils.isEmpty(params.get("allSubjects"))) {
            map.put("allSubjects ", "asb");
            Log.i("CourseListTask", "allSubjects>>>>" + params.get("allSubjects"));

        }
        if (!android.text.TextUtils.isEmpty(params.get("allGradeids"))) {
            map.put("allGradeids ", "ag");
            Log.i("CourseListTask", "allGradeids>>>>" + params.get("allGradeids"));

        }


        if (params.get("leaveType") != null) {
            map.put("course_type", params.get("leaveType"));
            Log.i("CourseListTask", "leaveType>>>>" + params.get("leaveType"));
        }

        if (params.get("typesTwoname") != null) {
            map.put("typesTwoname", params.get("typesTwoname"));
            Log.i("CourseListTask", "typesTwoname>>>>" + params.get("typesTwoname"));
        }
        map.put("productId", params.get("productId"));

    }

}
